define(['jquery', 'api', 'user', 'util'], function ($, api, user, util) {
    var vm,
    	wxid,
        controller = {
            init: function () {
            	if(user.isLogin()){
			        window.location.hash="#/";
			        return;
			    }
            	
				wxid=api.getWxid()?api.getWxid():'';
				api.headerShow(0);
				api.footerShow(0);
                api.setTitle(L('html_title_pass_getlogin'));
                this.render();
            },
            render: function () {
                var that = this;
                $('#common').html('<div id="getLogin"></div>');
                require(['text!pages/pass/getlogin.html', 'Vue'], function (tpl, Vue) {
                    vm = new Vue({
                        el: '#getLogin',
                        data: {
                            show: true,
                            step: 1,
                            form: {
                                mobile: '',
                                verifycode: '', // 手机短信码
                                captcha: '', // 图片验证码
                                loginpwd: '',
                                reloginpwd: ''
                            },
                            wxid:wxid,
                            imgSrc: false
                        },
                        methods: {
                            checkMobile: function () {
                                if (this.form.verifycode == '') {
                                    util.msg('请输入验证码！');
                                    return false;
                                }

                                api.pwdCheck({smcode: this.form.verifycode}, function (rs) {
                                    if(rs.retcode == '0'){
	                	              vm.step=2;
	                	        	}else if(rs.retcode=='43'){
	                        	 	 vm.step = 1;
	                        	     util.dealErrorCode(rs.retcode);
	                        	    }else{
//		                	        	   util.msg('帐号不存在！');
                                        util.dealErrorCode(rs.retcode);// 20161209dyx:增加提示信息
	                	        	}
                                });
                            },
                            getVerify: function () { // 获取手机短信码
                                if (this.form.captcha == '') {
                                    util.msg('请先输入动态码！');
                                } else {
                                    util.Verify.send(this.form.mobile, this.form.captcha, $('#btnVerify'));
                                }
                            },
                            resetPwd: function () {
                                var error = false;
                                if (typeof this.form.loginpwd == 'undefined' || this.form.loginpwd == '') {
                                    error = '请输入新密码';
                                } else if (typeof this.form.reloginpwd == 'undefined' || this.form.reloginpwd == '') {
                                    error = '请确认新登陆密码';
                                } else if (this.form.loginpwd !== this.form.reloginpwd) {
                                    error = '两次输入密码不一致！'
                                }else if(this.form.loginpwd.length<6){
                                	error = '密码必须大于6位'
                                }
                                if (error) {
                                    util.msg(error);
                                } else {
                                    var params = {wxid:vm.wxid,mobile:this.form.mobile, pwd:this.form.loginpwd, pwdtype: 49};
                                    api.resetPwd(params,function(rs){
                                        if(rs.retcode=='0'){
                                            util.msg('重置成功！');
                                            vm.form={
			                                    mobile: '',
			                                    verifycode: '', // 手机短信码
			                                    captcha: '', // 图片验证码
			                                    loginpwd: '',
			                                    reloginpwd: ''
			                                };
			                                
                                            setTimeout(function(){
                                            	vm.step = 1;
                                            	location.href = "#/";
                                            },300);
                                        }else{
                                            util.dealErrorCode(rs.retcode);
                                        }
                                        
                                    })
                                }
                            },
                            changeCaptcha: function () {
                                that.loadCaptcha();
                            }
                        },
                        template: tpl
                    });
                    that.loadCaptcha();//20161210dyx
                })
            },
            loadWxid:function(){
            	api.mobileToWxid({
            	    data:{mobile:vm.form.mobile},
            	    success:function(rs){
            	        if(rs.retcode=='0'){
            	        	if(rs.data.wxid!==''){
            	        	  vm.wxid=rs.data.wxid;
            	              vm.step=2;
            	        	}else if(rs.recode=='43'){
                    	 	 vm.step = 1;
                    	     util.dealErrorCode(rs.retcode);
                    	    }else{
            	        	   util.msg('帐号不存在！');
            	        	}
            	        }
            	    }
            	})
            },
            loadCaptcha: function () {
                api.captcha(function (rs) {
                    if (rs.retcode == '0') {
                        vm.imgSrc = 'data:image/png;base64,' + rs.imgcode;
                    }
                })
            },
            resetForm:function(){ //清除表单
	       		if(vm){
	       			for (i in vm.form) {
   	         			vm.form[i] = '';
   	         		}
	       		}
	       },
            onRouteChange: function () {
                if (vm){
                    vm.show = false;
                    vm.step = 1;
                    this.resetForm();
                }
            }
        };
    return controller;
})